//
//  TTHomeGridViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/11/20.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTHomeGridViewCell.h"
#import <UIImageView+WebCache.h>

@interface TTHomeGridViewCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *selloutImageView;    // 售罄
@property (nonatomic, strong) UILabel *titleLabel;  // 标题
@property (nonatomic, strong) UILabel *priceLabel;  // 价格
@property (nonatomic, strong) UILabel *kucunLabel;  // 库存



@end

@implementation TTHomeGridViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setupSubviews];
        
    }
    return self;
}

- (void)setModel:(TTHomeListModel *)model
{
    _model = model;
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.GoodsImage] placeholderImage:[UIImage imageNamed:@"f_placeholder_img"] options:SDWebImageRetryFailed];
    
    self.selloutImageView.hidden = (model.StockCount.intValue > 0 || model.CustomizedCount > 0);
    
    self.titleLabel.text = model.GoodsTitle;
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@", model.Price]];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:11] range:NSMakeRange(0, 1)];
    self.priceLabel.attributedText = attrStr;
    if (model.StockCount.intValue == 0) {
        self.kucunLabel.text = [NSString stringWithFormat:@"定制库存:%ld件", (long)model.CustomizedCount];
    } else {
        self.kucunLabel.text = [NSString stringWithFormat:@"库存:%@件", model.StockCount];
    }
    

}

- (void)setupSubviews
{
    self.bgView.frame = self.bounds;
    self.imageView.frame = CGRectMake(0, 0, self.width, 175 * kScale);
    self.selloutImageView.frame = self.imageView.bounds;
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView.mas_bottom).mas_offset(8);
        make.left.mas_equalTo(8);
        make.right.equalTo(self.contentView.mas_right).mas_offset(-8);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(8);
        make.left.equalTo(self.titleLabel);
    }];
    
    [self.kucunLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.priceLabel);
        make.right.equalTo(self.contentView.mas_right).mas_offset(-8);
    }];
    
    
}

- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 10;
        _bgView.layer.masksToBounds = YES;
        [self.contentView addSubview:_bgView];
    }
    return _bgView;
}

- (UIImageView *)imageView
{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        [self.bgView addSubview:_imageView];
        
    }
    return _imageView;
}

- (UIImageView *)selloutImageView
{
    if (!_selloutImageView) {
        _selloutImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"homelistsellout"]];
        _selloutImageView.contentMode = UIViewContentModeCenter;
        [self.bgView addSubview:_selloutImageView];
    }
    return _selloutImageView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithTitle:@"" color:[UIColor blackColor] fontSize:14];
        [self.bgView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [UILabel labelWithTitle:@"￥0.00" color:UIColorHex(FF0000) font:[UIFont boldSystemFontOfSize:16]];
        [self.bgView addSubview:_priceLabel];
    }
    return _priceLabel;
}

- (UILabel *)kucunLabel
{
    if (!_kucunLabel) {
        _kucunLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:10];
        [self.bgView addSubview:_kucunLabel];
    }
    return _kucunLabel;
}


@end
